package org.yankun.exam4j.core.question.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.yankun.exam4j.common.daobase.DaoBase;
import org.yankun.exam4j.core.question.service.QuestionService;
import org.yankun.exam4j.entity.CandidateInput;
import org.yankun.exam4j.entity.CandidateSelect;
import org.yankun.exam4j.entity.PaperQuestion;
import org.yankun.exam4j.entity.Question;
import org.yankun.exam4j.entity.QuestionTypeConfig;

@Transactional
@Service
public class QuestionServiceImpl implements QuestionService{

	@Resource
	private DaoBase<Question> dao;
	
	
	
	@Override
	public void addQuestion(Question question) {
		Assert.notNull(question, "Question must not be null");
		dao.getHibernateTemplate().save(question);
	}
	
	
	
	
	@Override
	public void addCandidateSelects(CandidateSelect cs) {
		Assert.notNull(cs, "CandidateSelect must not be null");
		dao.getHibernateTemplate().save(cs);
	}
	
	
	
	
	
	@Override
	public void addCandidateInput(CandidateInput ci) {
		Assert.notNull(ci, "CandidateInput must not be null");
		dao.getHibernateTemplate().save(ci);
	}
	
	
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PaperQuestion> getPaperQuestionByPaper(Serializable paperId) {
		String hql = "from PaperQuestion where paper.uid = ? order by question.questionTypeConfig";
		return dao.getHibernateTemplate().find(hql, paperId);
	}
	
	
	
	
	@Override
	public Question getQuestionByQuestionId(Serializable questionId) {
		return dao.getHibernateTemplate().get(Question.class, questionId);
	}




	@SuppressWarnings("unchecked")
	@Override
	public List<QuestionTypeConfig> getQuestionType() {
		String hql = " from QuestionTypeConfig ";
		return dao.getHibernateTemplate().find(hql);
	}




	@Override
	public void addPaperQuestion(PaperQuestion pq) {
		dao.getHibernateTemplate().save(pq);
	}

}
